package cn.jit.ssm.web.controller;

import cn.jit.ssm.service.JitBookService;
import cn.jit.ssm.support.JSONReturn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 图书处理模块
 *
 * @author: Cecurio
 * @create: 2017-12-14 8:53
 **/
@RestController
@RequestMapping(value = "/book")
public class JitBookController {
    final Logger logger = LoggerFactory.getLogger(JitBookController.class);

    @Autowired
    private JitBookService jitBookService;

    /**
     * 爬取豆瓣书籍到自己的数据库
     *
     * @param isbn 图书的isbn编号
     * @return
     */
    @RequestMapping("/crawler/{isbn}")
    @ResponseBody
    public JSONReturn bookCrawler(@PathVariable String isbn) {
        return jitBookService.insertSelective(isbn);
    }

    /**
     * 获取系统内的所有图书
     *
     * @return
     */
    @RequestMapping("/all")
    @ResponseBody
    public JSONReturn getAllBooks() {
        return jitBookService.getAllBooks();
    }

    /**
     * 分页获取图书
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条
     * @return
     */
    @RequestMapping("/page/{pageNum}/{pageSize}")
    @ResponseBody
    public JSONReturn getAllBook(@PathVariable("pageNum") String pageNum, @PathVariable("pageSize") String pageSize) {
        return jitBookService.getBooks(pageNum, pageSize);
    }

    /**
     * @param aoData
     * @return
     */
    @RequestMapping("/datatable")
    @ResponseBody
    // 使用@RequestParam注解 Parameter values are converted to the declared method argument type.
    public String getBook(@RequestParam String aoData) {
        logger.info("获取参数值:" + aoData);
        JSONArray jsonarray = JSONArray.fromObject(aoData);
        String sEcho = null;
        int iDisplayStart = 0;
        int iDisplayLength = 0;
        for (int i = 0; i < jsonarray.size(); i++) {
            JSONObject obj = (JSONObject) jsonarray.get(i);
            if (obj.get("name").equals("sEcho")) {
                sEcho = obj.getString("value");
            }
            if (obj.get("name").equals("iDisplayLength")) {
                iDisplayLength = obj.getInt("value");
            }
            if (obj.get("name").equals("iDisplayStart")) {
                iDisplayStart = obj.getInt("value");
            }
        }

        int pageNum = (iDisplayStart / iDisplayLength) + 1;
        return jitBookService.getBooKByDataTable(pageNum, iDisplayLength);
    }

    /**
     * 导出系统内全部书籍
     * @param response
     */
    @RequestMapping("/exportAsExcelFile")
    public void downloadBooks(HttpServletResponse response) {
        jitBookService.exportBookToExcel(response);
    }
}
